from image_worker import ImageWorker
import gtk, hildon
from defines import Defines

class MainMenu:
    def __init__(self, program, editor, widgets):
        self.program = program
        self.editor = editor
        
        # Fetch button widgets.
        effects_button = widgets.get_widget('Editor:EffectsButton')
        transform_button = widgets.get_widget('Editor:TransformButton')
        undo_button = widgets.get_widget('Editor:UndoButton')
        share_button = widgets.get_widget('Editor:ShareButton')
        cancel_button = widgets.get_widget('Editor:CancelButton')
        save_button = widgets.get_widget('Editor:SaveButton')
        
        # Connect signals and slots.
        effects_button.connect('clicked', self.effects_button_clicked)
        transform_button.connect('clicked', self.transform_button_clicked)
        undo_button.connect('clicked', self.undo_button_clicked)
        share_button.connect('clicked', self.share_button_clicked)
        cancel_button.connect('clicked', self.cancel_button_clicked)
        save_button.connect('clicked', self.save_button_clicked)
    
    def effects_button_clicked(self, widget):
        self.editor.main_vbox.hide()
        self.editor.effects_vbox.show()
    
    def transform_button_clicked(self, widget):
        self.editor.main_vbox.hide()
        self.editor.transform_vbox.show()
        
    def share_button_clicked(self, widget):
        self.editor.main_vbox.hide()
        self.editor.share_vbox.show()
        
    def undo_button_clicked(self, widget):
        self.editor.undo_operation()
    
    def cancel_button_clicked(self, widget):
        # The user has canceled the operations. Switch back to the image
        # browser window.
        self.program.set_mode('browse')
        
    def save_button_clicked(self, widget):
        # The user has acknowledged the applied operations. 
        # Let the user select a place to save the file.
        filename = self.__choose_file()
        if filename != None:
            # The user has selected an outfile.
            # If there are some operations that need to be applied to the image we 
            # start the worker thread now.
            if len(self.editor.pending_operations) != 0:
                worker = ImageWorker(self.editor.original_image, self.editor.pending_operations,
                                     self.program.task_monitor, filename)
                worker.start()
            # Otherwise simply write the file to disk now.
            else:
                # Validate the file name.
                index = filename.rfind('.')
                if index == -1 or len(filename[index+1:]) != 3:
                    # There is no extension - add one.
                    filename += '.jpg'
                elif not (filename[index+1:] in ('jpg','gif')):
                    # The format is unsupported. Switch to jpg.
                    filename = filename[:index] + '.jpg'
                try:
                    self.editor.original_image.save(filename)
                    self.program.popup_message("Image saved to %s."%filename)
                except Exception, e:
                    self.program.popup_message("Error saving to file.\n%s"%e.message)
        
    def __choose_file(self):
        filechooser = hildon.FileChooserDialog(self.editor.window, gtk.FILE_CHOOSER_ACTION_SAVE)
        filechooser.set_current_folder(Defines.IMAGE_PATH)
        status = filechooser.run()
        filechooser.hide()
        if status == gtk.RESPONSE_OK:
            return filechooser.get_filename()
        else:
            return None
